import allRouter from '../router/modules/goodsRouter'
import { constantRoutes } from '../router/index'
// const allRouters = allRouter
const getters = {
  top: state => state.menu.top,
  sidebar: state => state.app.sidebar,
  // size: state => state.app.size,
  // visitedViews: state => state.tagsView.visitedViews,
  // cachedViews: state => state.tagsView.cachedViews,
  // introduction: state => state.user.introduction,
  // roles: state => state.user.roles,
  permission: state => state.user.permission,
  permission_routes: state => state.permission.routes,
  systemId: state => state.settings.systemId,
  isAddRoutes: state => state.user.isAddRoutes, // 是否添加过路由
  roleRouter: state => { // 获取角色路由
    // let roleRouter = []
    // let permission = JSON.parse(state.user.permission)
    // allRouter.map(item => {
    //   if (permission.findIndex(item1 => item1 == item.meta.permission) != -1) {
    //     roleRouter.push(item)
    //   }
    // })
    let routers = constantRoutes.concat(deepClone(allRouter))
    // if (state.user.roleType == 1) { // 1最高权限管理员  
    //   routers = constantRoutes.concat(deepClone(allRouter))
    // } else if (state.user.roleType == 2) { // 2代理商
    //   let roleRouter = deepClone(allRouter)
    //   roleRouter.splice(roleRouter.findIndex(item => item.path == '/demand'), 1)
    //   routers = constantRoutes.concat(deepClone(allRouter))
    // }
    getMenuTree(routers, 1)
    return routers
  },
  // errorLogs: state => state.errorLog.logs
}


// 静态时调用
// 递归 更改字段名称
function getMenuTree (list, menuGrade) {
  list.map(item => {
      item.menuGrade = menuGrade
    if (item.children) {
      item.redirect = item.children[0].path
      let num = JSON.parse(JSON.stringify(menuGrade))+1
      getMenuTree(item.children, num)
    }
  })
}

/**
 * @param  {Array} userRouter 后台返回的用户权限json
 * @param  {Array} consRouter  前端配置好的所有动态路由的集合
 * @return {Array} realRouter 过滤后的路由
 */
 // 递归  获取动态路由
function getRealMenu(userRouter, consRouter, realRouter, path) {
  userRouter.map(item => { // 静态路由
    consRouter.map(item1 => { // 动态路由
      if (item.path == item1.path) {
        item1.meta.icon = item.icon || item1.meta.icon
        item1.sort = item.sort || ''
        item1.meta.title= item.name || ''
        item1.menuGrade= item.menuGrade
        item1.parentPath= path || ''
        realRouter.push(item1)
        if (item.children && item.children.length > 0) { // 有children 递归
          item1.redirect = item1.children[0].path
          // console.log(item1.children);
          // console.log(item1.children[0].path, '------------- item1.children[0].path');
          getRealMenu(item.children, item1.children, item1.children = [], item1.path)
        }
      } else if (item1.meta.activeMenu == item.path) {
        realRouter.push(item1)
      } 
      // 营销首页(单独判断 - 需引入组件形式展示页面) - (添加/编辑/三级页面)路由必须添加isAdd
      else if (item1.meta.activeMenu == '/marketingManage/index' && item1.isAdd && realRouter.findIndex(item2 => item2.path == item1.path) == -1) {
        realRouter.push(item1)
      }
    })
  })
}

/**
 *
 * @param  {Array} userRouter 后台返回的用户权限json
 * @param  {Array} allRouter  前端配置好的所有动态路由的集合
 * @return {Array} realRoutes 过滤后的路由
 */

// 对比后端动态路由权限
function recursionRouter (userRouter, allRouter) {
  var realRoutes = []
  getRealMenu(userRouter, allRouter, realRoutes)
  
  // console.log(realRoutes,'realRoutes-------------- ************')
  // 给一级菜单匹配默认跳转路径
  realRoutes.map(item => {
    if (item.children.length > 0) {
      item.redirect = item.children[0].path
      // 给二级菜单添加默认跳转路径
      item.children.map(item1 => {
        if (item1.children && item1.children.length > 0) {
          item1.redirect = item1.children[0].path
        }
      })
    }
  })
   realRoutes.sort((a,b)=>{ return b.sort-a.sort})
  return realRoutes
}
const deepClone = (obj, hash = new WeakMap()) => {
  if (obj === null || typeof obj !== 'object') return obj;
  if (obj instanceof Date) return new Date(obj);
  if (obj instanceof RegExp) return new RegExp(obj);
  if (hash.has(obj)) return hash.get(obj);

  const cloneObj = new obj.constructor();
  hash.set(obj, cloneObj);

  for (const key in obj) {
    if (obj.hasOwnProperty(key)) {
      cloneObj[key] = deepClone(obj[key], hash);
    }
  }

  return cloneObj;
};

export default getters


